import {
  // 导入所有的静态路由对象
  constantRoutes,
  // 导入所有的动态路由对象
  asyncRoutes
} from '@/router'
const state = {
  // 用来保存用户的菜单权限
  routes: constantRoutes // 默认所有用户都有静态路由的菜单权限
}
const mutations = {
  // newRoutes是登录用户的动态路由对象
  setRoutes(state, newRoutes) {
    // 应该是常量路由和动态路由进行合并
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  // 设置路由
  setRoutesAsync(context, {
    menus,
    mobile
  }) {
    // 根据menus和完整的动态路由筛选出当前登录用户的动态路由
    // menus:['settings', 'departments', 'saas-clients', 'employees', 'permissions']
    // asyncRoutes这个数组每个路由对象中的name属性和menus中选项进行关联
    let newRoute = []
    // 超级管理员拥有一切路由权限
    if (mobile === '13800000002') {
      newRoute = asyncRoutes
    } else {
      menus.forEach(key => {
        newRoute.push(...asyncRoutes.filter(item => item.name === key))
      })
    }
    context.commit('setRoutes', newRoute)
    // 将动态路由对象返回出去，实现动态路由的添加
    return newRoute
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
